package net.xxcl.stuManager.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;
import net.xxcl.stuManager.mapper.StudentMapper;
import net.xxcl.stuManager.pojo.Student;
import net.xxcl.stuManager.pojo.Teacher;

/**
 * @author 夏汛
 * 学生的相关操作
 */
public class StudentAction extends HttpServlet {
	
	private StudentMapper studentMapper = new StudentMapper();

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		if(request.getParameter("action").equalsIgnoreCase("list")){
			list(request, response);
		} else if (request.getParameter("action").equalsIgnoreCase("add")){
			add(request, response);
		} else if (request.getParameter("action").equalsIgnoreCase("getByid")){
			getByid(request, response);
		} else if (request.getParameter("action").equalsIgnoreCase("edit")){
			edit(request, response);
		} else if (request.getParameter("action").equalsIgnoreCase("del")){
			del(request, response);
		} 
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request, response);
	}
	
	/**
	 * 辅导员可以管理的学生列表
	 * @throws IOException 
	 */
	private void list(HttpServletRequest request, HttpServletResponse response) throws IOException {
		
		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		
		//前台传来的分页信息
		int page = Integer.parseInt(request.getParameter("page"));
		int rows = Integer.parseInt(request.getParameter("rows"));
		
		Teacher teacher = (Teacher)request.getSession().getAttribute("teacher");
		
		//设置sql语句所需要的参数
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("teacherid", teacher.getId());
		map.put("start", (page-1)*rows);
		map.put("count", rows);
		
		if(request.getParameter("queryName")!=null){
			map.put("queryName", "%"+request.getParameter("queryName")+"%");
		}
		
		List<Student> list = studentMapper.selectListByTeacherid(map);
		
		int total = studentMapper.countByTeacherid(map);
		//设置传到前台的信息
		Map<String,Object> responeMap = new HashMap<String, Object>();
		responeMap.put("total", total);
		responeMap.put("rows", list);
		JSONObject json = JSONObject.fromObject(responeMap);
		out.print(json);
		out.close();
	}
	
	/**
	 * 新增学生
	 * @throws IOException 
	 */
	private void add(HttpServletRequest request, HttpServletResponse response) throws IOException {		

		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		
		Map<String,Object> map = new HashMap<String, Object>();
		
		try {
			Student student = new Student();
			student.setAddr(request.getParameter("addr"));
			student.setBedroom(request.getParameter("bedroom"));
			student.setCard(request.getParameter("card"));
			student.setClassid(Integer.parseInt(request.getParameter("classid")));
			student.setIsmonitor(Integer.parseInt(request.getParameter("ismontor")));
			student.setJob(request.getParameter("job"));
			student.setName(request.getParameter("name"));
			student.setNumber(request.getParameter("number"));
			student.setPhone(request.getParameter("phone"));
			
			studentMapper.save(student);
			map.put("success", true);
		} catch (Exception e) {
			e.printStackTrace();
			map.put("msg", "新增学生失败！");
		}		
	
		JSONObject json = JSONObject.fromObject(map);
		out.print(json);
		out.close();
	}
	
	/**
	 * @param request
	 * @param response
	 * @throws IOException
	 * 根据id查找学生
	 */
	private void getByid(HttpServletRequest request, HttpServletResponse response) throws IOException {		

		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		
		int id = Integer.parseInt(request.getParameter("id"));
		Student student = studentMapper.selectById(id);
		student.setPassword("");
	
		JSONObject json = JSONObject.fromObject(student);
		out.print(json);
		out.close();
	}
	
	/**
	 * 修改学生
	 * @throws IOException 
	 */
	private void edit(HttpServletRequest request, HttpServletResponse response) throws IOException {		

		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		
		Map<String,Object> map = new HashMap<String, Object>();
		
		try {
			Student student = new Student();
			student.setId(Integer.parseInt(request.getParameter("id")));
			student.setAddr(request.getParameter("addr"));
			student.setBedroom(request.getParameter("bedroom"));
			student.setCard(request.getParameter("card"));
			student.setClassid(Integer.parseInt(request.getParameter("classid")));
			student.setIsmonitor(Integer.parseInt(request.getParameter("ismontor")));
			student.setJob(request.getParameter("job"));
			student.setName(request.getParameter("name"));
			student.setNumber(request.getParameter("number"));
			student.setPhone(request.getParameter("phone"));
			
			studentMapper.update(student);
			map.put("success", true);
		} catch (Exception e) {
			e.printStackTrace();
			map.put("msg", "修改学生失败！");
		}		
	
		JSONObject json = JSONObject.fromObject(map);
		out.print(json);
		out.close();
	}
	
	/**
	 * @param request
	 * @param response
	 * @throws IOException
	 * 根据id删除学生
	 */
	private void del(HttpServletRequest request, HttpServletResponse response) throws IOException {		

		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
				
		Map<String,Object> map = new HashMap<String, Object>();
		
		try {
			String ids = request.getParameter("ids");
			String[] temp = ids.split(","); 
			for (int i = 0; i < temp.length; i++) {
				studentMapper.deleteById(Integer.parseInt(temp[i]));
			}
			map.put("success", true);
			map.put("msg", "删除成功！");
		} catch (Exception e) {
			e.printStackTrace();
			map.put("msg", "删除失败！");
		}		
	
		JSONObject json = JSONObject.fromObject(map);
		out.print(json);
		out.close();
	}
}
